package com.webdiy.qq.impl;

import com.sys.tool.FileTool;
import com.sys.tool.HttpClientTool;
import com.sys.tool.StringTool;
import com.webdiy.dao.model.QqUserInfo;
import com.webdiy.qq.iface.DataCollectionService;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 2009-10-20
 * Time: 14:55:08
 * To change this template use File | Settings | File Templates.
 */
public class DataCollectionServiceImpl extends BaseQqServiceImpl implements DataCollectionService {

    public void insertQqCode(String data) {
        StringTool st = new StringTool();
        st.setMatcher(data);

        st.setPattern("\"QQ\":\"(\\d*)\"");
        String[] qqCode = st.getValues(1);

        st.setPattern("\"HA\":\"([^\"]*)\"");
        String[] qqImg = st.getValues(1);

        st.setPattern("\"GM\":\"(\\d*)\"");
        String[] qqH = st.getValues(1);

        st.setPattern("\"GN\":\"(\\d*)\"");
        String[] qqW = st.getValues(1);

        if ((qqCode.length == qqImg.length) && (qqH.length == qqW.length) && (qqCode.length == qqW.length)) {
            for (int i = 0; i < qqCode.length; i++) {
                QqUserInfo qui = new QqUserInfo();
                qui.setQqNo(qqCode[i]);
                qui.setPicture(qqImg[i].getBytes());
                qui.setWeightCode(qqW[i]);
                qui.setHeightCode(qqH[i]);

                if (dao.listByHql("FROM QqUserInfo WHERE qqNo = ?", qui.getQqNo()).size() > 0) {
                    log.warn("QQ号重复");
                } else {
                    dao.saveObject(qui);
                }

            }
        }

    }


    public void insertQqOtherInfo() {


        ExecutorService es = Executors.newFixedThreadPool(50);

        List<QqUserInfo> list = dao.listByHql("FROM QqUserInfo WHERE birth is null");

        for (final QqUserInfo q : list) {
            es.execute(new Runnable() {
                public void run() {
                    //QqUserInfo q = (QqUserInfo) dao.getObject(QqUserInfo.class, "402881d72486746701248677577e000d");
                    StringTool st = new StringTool();
                    HttpClientTool hct = new HttpClientTool();
                    hct.setUrl("http://city.qzone.qq.com/json.php?mod=profile&act=guest&callback=showUserInfo&uin=" + q.getQqNo());
                    String body = hct.getInnerHTML();
                    st.setMatcher(body);
                    st.setPattern("\"birthday\":\"([^\"]*)\"");
                    q.setBirth(st.getValue(1));


                    
                    st.setPattern("\"sex\":(\\d*),");
                    if (st.getValue(1) != null) {
                    q.setSex(st.getValue(1));
                    }else{
                        st.setPattern("\"sex\":\"(\\d*)\"");
                        q.setSex(st.getValue(1));
                    }


                    
                    st.setPattern("\"job\":(\\d*),");
                    if (st.getValue(1) != null) {
                    q.setJobCode(st.getValue(1));
                    }else{
                        st.setPattern("\"job\":\"(\\d*)\"");
                        q.setJobCode(st.getValue(1));
                    }


                    st.setPattern("\"bloodType\":(\\d*),");
                    if (st.getValue(1) != null) {
                        q.setBloodTypeCode(st.getValue(1));
                    } else {
                        st.setPattern("\"bloodType\":\"(\\d*)\"");
                        q.setBloodTypeCode(st.getValue(1));
                    }


                    st.setPattern("\"edu\":(\\d*),");
                    if (st.getValue(1) != null) {
                        q.setEduCode(st.getValue(1));
                    } else {
                        st.setPattern("\"edu\":\"(\\d*)\"");
                        q.setEduCode(st.getValue(1));
                    }


                    dao.updateObject(q);
                }
            });
        }
        es.shutdown();
    }


}
